// pages/searchShop/searchShop.js
import {
  HTTP
} from '../../utils/http-p.js'
const http = new HTTP;

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:'',
    shopList: [],
    isFlag:true,
    isType:'1',
    hotList: [{ name: '大闸蟹' }, { name: '五常大米' }, { name: '法式鹅肝酱' }, { name: '红豆' }, { name: '面膜' }, { name: '鱼子酱' }],
    historySearchList:[]

  },

 

  onChange(e) {
    this.setData({
      value: e.detail
    });
  },

  onSearch() {
    const word = this.data.value
    this.searchShop(word);
  },

  onClick() {
    console.log(this.data.value)
    const word = this.data.value
    this.searchShop(word);
  },

  /**
   * 分词搜索
   * @param {value} options 
   */
  searchShop(word){
    if(word==''){
        return
    }
    
    const historySearchList = wx.getStorageSync('historySearchList') || []
    const flag = this.historySearchIsFlag(word,historySearchList)
    if(!flag){
      historySearchList.unshift({name:word})
      wx.setStorageSync('historySearchList', historySearchList)

    }
    http.request({
      url: 'product-info/searchProduct',
      data:{name:word},
      success: (res) => {
        const list = res.data;
        this.setData({
          shopList: list,
          isFlag:false
        })
      }
    })  
  },
  historySearchIsFlag(word, historySearchList){
    let arr = []
    if (historySearchList.length > 6){
      arr = historySearchList.slice(0,6)
    }else{
      arr = historySearchList
    }
    for (let i = 0; i < arr.length;i++) {
      const name = arr[i].name
      if(word == name){
        return true
        break
      }
    }
    return false
  },


  searchShopName(event){
    const word = event.currentTarget.dataset.name
    this.searchShop(word);
  },
  goToProductInfo(event){
    const productId = event.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/shopInfo/shopInfo?productId=${productId}`
    })
  },
  queryShopList(categoryType) {
    http.request({
      url: 'product-info/getAll',
      data: {
        categoryType: categoryType
      },
      success: (res) => {

        const list = res.data;
        console.log(list)
        this.setData({
          shopList: list,
        })
      }
    })

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    const isType = options.isType
    const categoryType = options.categoryType
    console.log(isType)
    if (isType == 2){
      this.setData({
        isType:2,
        isFlag:false
      })
      this.queryShopList(categoryType)
    }else{
      const arr = wx.getStorageSync('historySearchList') || []
      const historySearchList = arr.slice(0,6)
      console.log(historySearchList)
      this.setData({
        historySearchList
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})